package com.hlingsoft.cms.action;

import com.hlingsoft.cms.dao.*;
;
import com.hlingsoft.cms.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.*;

/**
 * Created by selbylei on 2015/5/28.
 */
@Controller
@RequestMapping("/message")
public class MessageAction extends BaseAction {

    @Autowired
    private CommentMessageDao commentMessageDao;

    @Autowired
    private LikesMessageDao likesMessageDao;

    @Autowired
    private FocusMessageDao focusMessageDao ;

    @Autowired
    private SystemMessageDao systemMessageDao;

    @Autowired
    private ImageDao imageDao;

    @Autowired
    private AppUserDao appUserDao;

    @Autowired
    private  MessageStatusDao messageStatusDao;

    @Autowired
    private FocusDao focusDao;

    private  final int  rows=10;



    /**
     * @param type    1.评论我的人
     *                2.赞我的人
     *                3.关注我的人
     *                4.公告
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "appUserId") long appUserId,
            @RequestParam(value = "type") int type,
            @RequestParam(value = "pageNum") int pagNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            switch (type) {
                case 1: {
                    List<CommentMessage> commentMessageList = commentMessageDao.getCommentMessageByToId(appUserId,0,20);
                    for (CommentMessage commentMessage:commentMessageList){
                        List<Image> imageList = imageDao.getImageByNewsId(commentMessage.getNewsId());
                        String newsImage = imageList.get(0).getPath();
                        commentMessage.setNewsImage(newsImage);
                        commentMessage.setMyImageHead(appUserDao.getUserById(commentMessage.getMyId()).getHeadImage());
                        commentMessage.setMyNickname(appUserDao.getUserById(commentMessage.getMyId()).getNickname());
                        commentMessage.setToImageHead(appUserDao.getUserById(appUserId).getHeadImage());
                    }
                    messageStatusDao.readCommentMessage(appUserId);
                    resultMap.put("commentMessageList", commentMessageList);
                    resultMap.put("result", true);
                    break;
                }
                case 2: {
                    List<LikesMessage> likesMessageList = likesMessageDao.getLikesMessageByToId(appUserId,0,20);
                    for (LikesMessage likesMessage:likesMessageList){
                        List<Image> imageList = imageDao.getImageByNewsId(likesMessage.getNewsId());
                        String newsImage = imageList.get(0).getPath();
                        likesMessage.setNewsImage(newsImage);
                        likesMessage.setMyImageHead(appUserDao.getUserById(likesMessage.getMyId()).getHeadImage());
                        likesMessage.setMyNickname(appUserDao.getUserById(likesMessage.getMyId()).getNickname());
                        likesMessage.setToImageHead(appUserDao.getUserById(appUserId).getHeadImage());
                    }
                    messageStatusDao.readLikesMessage(appUserId);
                    resultMap.put("likesMessageList", likesMessageList);
                    resultMap.put("result", true);
                    break;
                }
                case 3: {
                    List<FocusMessage> focusMessageList  = focusMessageDao.getFocusMessageByToId(appUserId,0,20);
                    for (FocusMessage focusMessage:focusMessageList){
                        AppUser my = appUserDao.getUserById(focusMessage.getMyId());
                        focusMessage.setMyImageHead(my.getHeadImage());
                        focusMessage.setMyNickname(my.getNickname());

                        if (focusDao.getFocusByIds(appUserId,my.getId())==null){
                            focusMessage.setIsFocus(0);
                        } else {
                            focusMessage.setIsFocus(1);
                        }
                    }
                    messageStatusDao.readFocusMessage(appUserId);
                    resultMap.put("focusMessageList", focusMessageList);
                    resultMap.put("result", true);
                    break;
                }
                case 4: {
                    List<SystemMessage> systemMessageList = systemMessageDao.getSystemMessage(0,20);
                    messageStatusDao.readSystemMessage(appUserId);
                    resultMap.put("systemMessageList",systemMessageList);
                    resultMap.put("result",true);
                    break;
                }

                default: {
                    resultMap.put("msg", "请给出正确的类型");
                    resultMap.put("result", false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }
}



